package com.zoe.onelink.core.autoconfigure;

import com.zoe.onelink.core.property.OnelinkProperties;
import com.zoe.onelink.core.property.http.RestTemplateConfig;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * <p>标题: </p>
 * <p>描述: </p>
 * <p>版权: Copyright (c) 2021</p>
 * <p>公司: 智业软件股份有限公司</p>
 *
 * @version: 1.0
 * @author: ruansheng
 * @date: 2021-08-10
 */
@Configuration(proxyBeanMethods = false)
public class OnelinkRestTemplateAutoConfiguration {


    @Bean
    public static RestTemplate restTemplate(List<ClientHttpRequestInterceptor> clientHttpRequestInterceptors,
                                            OnelinkProperties onelinkProperties) {

        RestTemplateConfig restTemplateProperties = onelinkProperties.getHttp().getRestTemplate();
        SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
        requestFactory.setConnectTimeout(restTemplateProperties.getConnectTimeout());
        requestFactory.setReadTimeout(restTemplateProperties.getReadTimeout());
        requestFactory.setChunkSize(restTemplateProperties.getChunkSize());
        requestFactory.setBufferRequestBody(restTemplateProperties.isBufferRequestBody());
        requestFactory.setOutputStreaming(restTemplateProperties.isOutputStreaming());

        RestTemplate restTemplate = new RestTemplate();
        restTemplate.setRequestFactory(requestFactory);
        restTemplate.setInterceptors(clientHttpRequestInterceptors);
        return restTemplate;
    }


}
